﻿using System;
using System.Collections.Generic;
using System.Runtime.Remoting.Messaging;
using MySql.Data.MySqlClient;
using OrionApplication.OrionLib.Orion;

namespace OrionApplication.OrionLib.Dao
{
    public class DaoOrionMessage : DaoBase
    {
        public DaoOrionMessage()
            : base()
        {
            this.TableName = "OrionMessage";
            this.ViewName = "VOrionMessage";
        }

        public OrionMessage SelectById(Int32 OrionId)
        {
            lParam.Clear();
            OrionMessage retour = new OrionMessage();
            String req = this.DebutSelect
                + this.TableName
                + " WHERE OrionId=@orionid";

            lParam.Add(new MySqlParameter("@orionid", OrionId));

            MySqlDataReader dr = this.ExecuteQueryReader(req, lParam);

            if (dr.Read())
                retour = ChargeFromReader(dr);
            dr.Close();
            return retour;
        }

        public List<OrionMessage> SelectAll()
        {
            lParam.Clear();
            List<OrionMessage> retour = new List<OrionMessage>();
            String req = this.DebutSelect 
                + this.TableName;

            MySqlDataReader dr = this.ExecuteQueryReader(req);

            while (dr.Read())
                retour.Add(ChargeFromReader(dr));
            dr.Close();
            return retour;
        }

        public OrionMessage Insert(OrionMessage om)
        {
            lParam.Clear();
            om.OrionCrea = DateTime.Now;
            om.OrionMaj = DateTime.Now;

            OrionMessage retour = new OrionMessage();
            String req = "INSERT INTO " + TableName + "(OrionId, IdentMessage, Message, OrionCrea, OrionMaj, OrionUserId, OrionEtat) " +
                "VALUES(NULL, @IdentMessage, @Message, @OrionCrea, @OrionMaj, @OrionUserId, @OrionEtat)";

            lParam.Add(new MySqlParameter("@IdentMessage",om.IdentMessage));
            lParam.Add(new MySqlParameter("@Message", om.Message));
            lParam.Add(new MySqlParameter("@OrionCrea",om.OrionCrea));
            lParam.Add(new MySqlParameter("@OrionMaj", om.OrionMaj));
            lParam.Add(new MySqlParameter("@OrionUserId", ((OrionUser)CallContext.GetData(OrionConfBase.OrionUserConnected)).OrionId));
            lParam.Add(new MySqlParameter("@OrionEtat", 1));

            this.ExecuteNonQuery(req, lParam);
            retour = this.SelectById(Convert.ToInt32(this.LastInsertId));

            return retour;
        }

        public OrionMessage Update(OrionMessage om)
        {
            lParam.Clear();
            
            OrionMessage retour = new OrionMessage();
            String req = "UPDATE " + TableName + " SET IdentMessage=@IdentMessage, Message=@Message, OrionMaj=@OrionMaj, OrionUserId=@OrionUserId " +
                " WHERE OrionId=@OrionId";

            lParam.Add(new MySqlParameter("@OrionId", om.OrionId));
            lParam.Add(new MySqlParameter("@IdentMessage", om.IdentMessage));
            lParam.Add(new MySqlParameter("@Message", om.Message));
            lParam.Add(new MySqlParameter("@OrionMaj", om.OrionMaj));
            lParam.Add(new MySqlParameter("@OrionUserId", ((OrionUser)CallContext.GetData(OrionConfBase.OrionUserConnected)).OrionId));
            lParam.Add(new MySqlParameter("@OrionEtat", 1));

            this.ExecuteNonQuery(req, lParam);
            retour = this.SelectById(om.OrionId);

            return retour;
        }

        public OrionMessage SelectByIdent(String IdentMessageP, bool ActifP = true)
        {
            lParam.Clear();
            OrionMessage retour = new OrionMessage();
            String req = this.DebutSelect
                + this.TableName
                + " WHERE IdentMessage=@IdentMessage AND OrionEtat=@OrionEtat";

            lParam.Add(new MySqlParameter("@IdentMessage", IdentMessageP));
            lParam.Add(new MySqlParameter("@OrionEtat", ActifP));

            MySqlDataReader dr = this.ExecuteQueryReader(req, lParam);

            if (dr.Read())
                retour = ChargeFromReader(dr);
            dr.Close();
            return retour;
        }

        private OrionMessage ChargeFromReader(MySqlDataReader dr)
        {
            OrionMessage retour = new OrionMessage();
            retour.OrionId = dr.GetInt32("OrionId");

            if (dr.IsDBNull(1))
                retour.IdentMessage = string.Empty;
            else
                retour.IdentMessage = dr.GetString("IdentMessage");
            if (dr.IsDBNull(2))
                retour.Message = string.Empty;
            else
                retour.Message = dr.GetString("Message");

            retour.OrionCrea = dr.GetDateTime("OrionCrea");
            retour.OrionMaj = dr.GetDateTime("OrionMaj");
            retour.OrionUserId = dr.GetInt32("OrionUserId");
            retour.OrionEtat = dr.GetBoolean("OrionEtat");

            return retour;
        }
    }
}
